package com.sun.encrypt;


import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Test {
    private static final String UTF8 = "UTF-8";
    @Test
    public void test01(){}

    /**
     * jdk base64加解密
     */
    @Test
    public void test02(){
        String str = "测试一下";
        try {
            //加密
            String encode = Base64.getEncoder().encodeToString(str.getBytes(UTF8));
            System.out.println(encode);
            //解密
            byte[] decode = Base64.getDecoder().decode(encode.getBytes(UTF8));
            System.out.println(new String(decode,UTF8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test03() throws UnsupportedEncodingException {
        String str = "测试一下";
        String encodeBase64String = org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes(StandardCharsets.UTF_8));
        System.out.println(encodeBase64String);
        byte[] bytes = org.apache.commons.codec.binary.Base64.decodeBase64(encodeBase64String.getBytes(StandardCharsets.UTF_8));
        String s = new String(bytes, UTF8);
        System.out.println(s);
    }

}
